﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using COFE.Core.Filter;
using COFE.Core.Utils;
using System.IO;

namespace COFE.Core.EntryType
{
    public class EntryIconExtractor : IEntryTypeIconExtractor
    {
        public byte[] ConstructIcon(IEntryFilter[] typeFilters, IconProperties size)
        {
            Size defSize = size.GetDefaultSize();
            Bitmap retVal = new Bitmap(defSize.Width, defSize.Height);

            using (Graphics g = Graphics.FromImage(retVal))
            foreach (var tf in typeFilters)
            {
                var eip = FilterUtils2<IEntryFilter>.GetIconProvider(tf);
                if (eip != null)
                {
                    var iconBitmap = new Bitmap(new MemoryStream(eip.GetIconStream(tf, size)));                    
                    if (iconBitmap != null)
                    {
                        var drawPos = new PointF(Math.Max(0, defSize.Width - iconBitmap.Width),
                            Math.Max(0, defSize.Height - iconBitmap.Height));
                        g.DrawImage(iconBitmap, drawPos);
                    }
                }
            }

            return retVal.ConvertToBitmapArray();
        }

       
    }
}
